<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>
<style>
</style>
<script type="text/javascript">
    var leaveDataGrid;
    $(function() {
        leaveDataGrid = $('#leaveDataGrid').datagrid({
            url : '${path}/example/leave/dataGrid',
            striped : true,
            rownumbers : true,
            pagination : true,
            singleSelect : true,
            idField : 'id',
            sortName : 'id',
            sortOrder : 'asc',
            pageSize : 20,
            pageList : [ 10, 20, 30, 40, 50, 100, 200, 300, 400, 500 ],
            columns : [ [
            	{
                    width : '100',
                    title : '流程实例ID',
                    field : 'processInstanceId'
                },
                {
                width : '200',
                title : '标题',
                field : 'title'
            }, {
                    width : '100',
                    title : '审批状态',
                    field : 'status',
                    formatter:function(value,row,index){
                        switch(value){
                            case -1:
                                return '暂存'
                            case 1:
                                return '<span style="color:red;">审批中</span>'
                            case 3:
                                return '<span style="color:green;">审批通过</span>'
                            case 5:
                                return '审批失败'
                            case 2:
                                return '审批调整'
                            case 9:
                                return '作废'
                        }
                    }
                },{
                    width : '100',
                    title : '当前任务',
                    field : 'taskName'
                }, {
                    width : '100',
                    title : '任务执行人',
                    field : 'assigneeOrCandidateUser'
                },
                {
                width : '100',
                title : '请假时间',
                field : 'leaveDate'
                ,formatter : function(value, row, index) {
                	if(value==null){
               	   	 return "";
               	 	}
                    var date=new Date(value);
	               	 var y = date.getFullYear();
	               	 var m = date.getMonth() + 1;
	               	 if(m < 10) m = "0" + m;
	               	 var d = date.getDate();
	               	 if(d < 10) d = "0" + d;
	               	 var h = date.getHours();
	               	 if(h < 10) h = "0" + h;
	               	 var min = date.getMinutes();
	               	 if(min < 10) min = "0" + min;
	               	 var s=date.getSeconds();
	               	 if(s<10)s="0"+s;
                     return y+"/"+m+"/"+d;
                 }
            },
                {
                width : '100',
                title : '请假天数',
                field : 'days'
            } ,{
                width : '100',
                title : '请假原因',
                field : 'reason'
            }
              
            ]],
            frozenColumns : [ [{
                field : 'action',
                title : '操作',
                width : 340,
                formatter : function(value, row, index) {
                    var str = '';
                    if(row.status!=-1) {
                        str += $.formatString('<a href="javascript:void(0)" class="leave-easyui-linkbutton-comment" data-options="plain:true,iconCls:\'fi-eye icon-green\'" onclick="queryHistoryCommentFun(\'{0}\');" >历史批注</a>', row.processInstanceId);
                        str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                        str += $.formatString('<a href="javascript:void(0)" class="leave-easyui-linkbutton-cancel" data-options="plain:true,iconCls:\'fi-trash icon-red\'" onclick="cancelleaveFun(\'{0}\');" >作废</a>', row.processInstanceId);
                        str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                        str += $.formatString('<a href="javascript:void(0)" class="leave-easyui-linkbutton-view" data-options="plain:true,iconCls:\'fi-magnifying-glass\'" onclick="viewleaveFun(\'{0}\');" >查看</a>', row.processInstanceId);
                        if(row.status==1){
                            str += $.formatString('<a href="javascript:void(0)" class="leave-easyui-linkbutton-set" data-options="plain:true,iconCls:\'fi-widget \'" onclick="setTaskAssignee(\'{0}\');" >设置任务执行人</a>', row.processInstanceId);
                        }
                    }else{
                        str += $.formatString('<a href="javascript:void(0)" class="leave-easyui-linkbutton-submit" data-options="plain:true,iconCls:\'fi-check\'" onclick="submitProcessInstanceFun(\'{0}\');" >提交</a>', row.id);
                        str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                        str += $.formatString('<a href="javascript:void(0)" class="leave-easyui-linkbutton-edit" data-options="plain:true,iconCls:\'fi-pencil icon-blue\'" onclick="editleaveFun(\'{0}\');" >编辑</a>', row.id);
                        str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
                        str += $.formatString('<a href="javascript:void(0)" class="leave-easyui-linkbutton-del" data-options="plain:true,iconCls:\'fi-x icon-red \'" onclick="deleteleaveFun(\'{0}\');" >删除</a>', row.id);

					}
                    return str;
                }
            } ] ],
            onLoadSuccess:function(data){
                $('.leave-easyui-linkbutton-comment').linkbutton({text:'历史批注'});
                $('.leave-easyui-linkbutton-cancel').linkbutton({text:'作废'});
                $('.leave-easyui-linkbutton-view').linkbutton({text:'查看'});
                $('.leave-easyui-linkbutton-submit').linkbutton({text:'提交'});
                $('.leave-easyui-linkbutton-edit').linkbutton({text:'编辑'});
                $('.leave-easyui-linkbutton-del').linkbutton({text:'删除'});
                $('.leave-easyui-linkbutton-set').linkbutton({text:'设置任务执行人'});
            },
            toolbar : '#leaveToolbar'
        });
    });

    function submitProcessInstanceFun(id) {
		$.ajax({
			url:'${path}/example/leave/submitProcessInstance',
			data:{"id":id},
			dataType:'json',
			type:'post',
			success:function(data){
			    if(data.success){
			        showMsg(data.msg);
			        leaveDataGrid.datagrid('reload');
                }else{
			        parent.$.messager.alert('错误',data.msg,'error');
                }
            }
		})
    }
    function viewleaveFun(processInstanceId){
    	var title="查看";
		 var url="${ctxPath}/example/leave/viewleaveinfo/"+processInstanceId;
		  var dialog= $('<div>').dialog({
		        title:title,
		        href:url,
		        width : '80%',
		        height : '80%',
		        modal:true,
		        onClose : function() {
		            $(this).dialog('destroy');
		        },
		        buttons:[{
		            text:'关闭',
		            handler:function () {
		            	dialog.dialog('close');
		            }
		        }]
		  })
    }
    function queryHistoryCommentFun(processInstanceId){
    	var dialog=$('<div>').dialog({
            title : '查看历史批注',
            width : '80%',
            height : '80%',
            modal:true,
            onClose : function() {
		          $(this).dialog('destroy');
		      },
            href : '${path}/process/getCommentsByProcessInstanceId?processInstanceId='+processInstanceId,
            buttons : [ {
                text : '关闭',
                handler : function() {
                	dialog.dialog('close');
                }
            } ]
        });
    }
    function addleaveFun() {
        $.modalDialog({
            title : '添加',
            width : '80%',
            height : '80%',
            href : '${path}/example/leave/toStart',
            buttons : [ {
                text : '暂存',
                handler : function() {
                    tempStoage();
                    parent.$.modalDialog.openner_dataGrid = leaveDataGrid;//因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
                    var f = parent.$.modalDialog.handler.find('#leaveAddForm');
                    f.submit();
                }
            } ,{
                text : '提交',
                handler : function() {
                    startProcessInstance();
                    parent.$.modalDialog.openner_dataGrid = leaveDataGrid;//因为添加成功之后，需要刷新这个treeGrid，所以先预定义好
                    var f = parent.$.modalDialog.handler.find('#leaveAddForm');
                    f.submit();
                }
            }]
        });
    }

    function editleaveFun(id) {
        if (id == undefined) {
            var rows = leaveDataGrid.datagrid('getSelections');
            id = rows[0].id;
        } else {
            leaveDataGrid.datagrid('unselectAll').datagrid('uncheckAll');
        }
        parent.$.modalDialog({
            title : '编辑',
            width : '80%',
            height : '80%',
            href : '${path}/example/leave/editPage?id=' + id,
            buttons : [ {
                text : '确定',
                handler : function() {
                    parent.$.modalDialog.openner_dataGrid = leaveDataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
                    var f = parent.$.modalDialog.handler.find('#leaveEditForm');
                    f.submit();
                }
            } ]
        });
    }

    function cancelleaveFun(processInstanceId) {
        if (processInstanceId == undefined) {//点击右键菜单才会触发这个
            var rows = leaveDataGrid.datagrid('getSelections');
            processInstanceId = rows[0].processInstanceId;
        } else {//点击操作里面的删除图标会触发这个
            leaveDataGrid.datagrid('unselectAll').datagrid('uncheckAll');
        }
        parent.$.messager.confirm('询问', '您是否要作废吗？', function(b) {
            if (b) {
                progressLoad();
                $.post('${path}/example/leave/cancel?processInstanceId='+processInstanceId, {}
                , function(result) {
                    if (result.success) {
                        parent.$.messager.alert('提示', result.msg, 'info');
                        leaveDataGrid.datagrid('reload');
                    }else{
                    	parent.$.messager.alert('提示', result.msg, 'info');
                    }
                    progressClose();
                }, 'JSON');
            }
        });
    }

    function deleteleaveFun(id) {
        if (id == undefined) {//点击右键菜单才会触发这个
            var rows = leaveDataGrid.datagrid('getSelections');
            processInstanceId = rows[0].id;
        } else {//点击操作里面的删除图标会触发这个
            leaveDataGrid.datagrid('unselectAll').datagrid('uncheckAll');
        }
        parent.$.messager.confirm('询问', '您是否要删除吗？', function(b) {
            if (b) {
                progressLoad();
                $.post('${path}/example/leave/delete?id='+id, {}
                , function(result) {
                    if (result.success) {
                        parent.$.messager.alert('提示', result.msg, 'info');
                        leaveDataGrid.datagrid('reload');
                    }else{
                    	parent.$.messager.alert('提示', result.msg, 'info');
                    }
                    progressClose();
                }, 'JSON');
            }
        });
    }

    function searchleaveFun() {
    	leaveDataGrid.datagrid('load', $.serializeObject($('#searchleaveForm')));
    }
    function cleanleaveFun() {
        $('#searchleaveForm input').val('');
        $('#searchleaveForm')[0].reset();
       leaveDataGrid.datagrid('load', {});
    }


</script>
<div class="easyui-layout" data-options="fit:true,border:false">
   <div data-options="region:'north',border:false" style=" overflow: hidden;background-color: #fff">
        <form id="searchleaveForm">
            <table>
                <tr>
              <td>审批状态</td>
			  <td>
				<select name="status" class="easyui-text">
					<option value="">请选择</option>
					<option value="-1">暂存</option>
					<option value="1">审批中</option>
					<option value="3">审批通过</option>
					<option value="5">审批失败</option>
					<option value="2">审批调整</option>
					<option value="9">作废</option>
				</select>
			 </td>

              <td>标题</td>
			  <td>
				<input name="title"   type="text" placeholder="请输入标题" class="easyui-text  span2" >
			 </td>

              <td>流程实例ID</td>
			  <td>
				<input name="processInstanceId"   type="text" placeholder="请输入流程实例ID" class="easyui-text  span2" >
			 </td>

              <td>请假时间</td>
			  <td>
<input name="leaveDateStart"  type="text"  class="easyui-text easyui-datebox  span2">
~<input name="leaveDateEnd"  type="text"  class="easyui-text easyui-datebox  span2">
</td>
<td>
  <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'fi-magnifying-glass',plain:true" onclick="searchleaveFun();">查询</a>
<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'fi-x-circle',plain:true" onclick="cleanleaveFun();">清空</a>
</td></tr>

            </table>
        </form>
    </div>
    <div data-options="region:'center',border:true">
        <table id="leaveDataGrid" data-options="fit:true,border:false"></table>
    </div>
</div>
<div id="leaveToolbar" style="display: none;">
    <a onclick="addleaveFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'fi-plus icon-green'">添加</a>
</div>
